package edu.uoc.tdp.client.proves;

import edu.uoc.tdp.beans.proves.EstatProva;
import edu.uoc.tdp.beans.proves.Prova;
import edu.uoc.tdp.common.EOlympicsException;
import edu.uoc.tdp.common.RemotProvesInterface;
import edu.uoc.tdp.common.TDSLanguageUtils;
import java.awt.Font;
import java.awt.event.WindowEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFbuscadorProves.java
 *
 * Created on 06-may-2012, 15:54:03
 */
/**
 *
 * @author xmarti
 */
public class PantallaBuscadorProves extends JFrame {
    private JTable JTaulaProves;
    private JButton jBBuscar;
    private JButton jBSortir;
    private JButton jBVeure;
    private JComboBox jCEstat;
    private JLabel jLEstat;
    private JLabel jLProva;
    private JLabel jLdataFiInscripcio;
    private JLabel jLdataIniInscripcio;
    private JPanel jPanelBotons;
    private JPanel jPanelEstat;
    private JPanel jPanelFiltre;
    private JPanel jPanelInscripcio;
    private JPanel jPanelProva;
    private JScrollPane jScrollPane1;
    private JTextField jTDescricpcio;
    private JTextField jTIdProva;
    private JTextField jTextFiInscripcio;
    private JTextField jTextIniInscripcio;
    Font fontEtiqueta;
    private String url="rmi://localhost/RemotProvesImpl";
    private RemotProvesInterface remotProves;
    
    public PantallaBuscadorProves() {
        initComponents();
        
        this.setLocation(100, 100);
    }

   
   
    private void initComponents() {

        jPanelProva = new JPanel();
        jLProva = new JLabel();
        jTIdProva = new JTextField();
        jTDescricpcio = new JTextField();
        jPanelInscripcio = new JPanel();
        jLdataIniInscripcio = new JLabel();
        jTextIniInscripcio = new JTextField();
        jLdataFiInscripcio = new JLabel();
        jTextFiInscripcio = new JTextField();
        jPanelEstat = new JPanel();
        jLEstat = new JLabel();
        jCEstat = new JComboBox();
        jPanelBotons = new JPanel();
        jBBuscar = new JButton();
        jBVeure = new JButton();
        jBSortir = new JButton();
        jPanelFiltre = new JPanel();
        jScrollPane1 = new JScrollPane();
        JTaulaProves = new JTable();
        fontEtiqueta=new Font("Tahoma", 0, 11);
        
        jLProva .setFont(fontEtiqueta);jLProva.setHorizontalAlignment(SwingConstants.RIGHT);
        jTIdProva .setFont(fontEtiqueta);jTIdProva.setHorizontalAlignment(SwingConstants.RIGHT); 
        jTDescricpcio .setFont(fontEtiqueta);jTDescricpcio.setHorizontalAlignment(SwingConstants.RIGHT); 

        jLdataIniInscripcio .setFont(fontEtiqueta);jLdataIniInscripcio .setHorizontalAlignment(SwingConstants.RIGHT);
        jTextIniInscripcio .setFont(fontEtiqueta);jTextIniInscripcio.setHorizontalAlignment(SwingConstants.RIGHT); 
        jLdataFiInscripcio .setFont(fontEtiqueta);jLdataFiInscripcio .setHorizontalAlignment(SwingConstants.RIGHT); 
        jTextFiInscripcio .setFont(fontEtiqueta);jTextFiInscripcio.setHorizontalAlignment(SwingConstants.RIGHT); 

        jLEstat .setFont(fontEtiqueta);jLEstat .setHorizontalAlignment(SwingConstants.RIGHT); 
        jCEstat .setFont(fontEtiqueta);

        jBBuscar .setFont(fontEtiqueta);jBBuscar.setHorizontalAlignment(SwingConstants.RIGHT); 
        jBVeure .setFont(fontEtiqueta);jBVeure .setHorizontalAlignment(SwingConstants.RIGHT); 
        jBSortir .setFont(fontEtiqueta);jBSortir.setHorizontalAlignment(SwingConstants.RIGHT); 

        ////jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(Universitat.values()));
        jCEstat.setModel( new DefaultComboBoxModel(EstatProva.values()));          
            
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {            
            public void windowClosing(java.awt.event.WindowEvent evt) {
                tancarFinestra(evt);
            }
        });

        //disparadors

        jBVeure.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    AccioVeureProva(evt);
                } catch (ParseException ex) {
                    ex.getMessage();
                }
            }
        });

       
        

        pintarEtiquetes();
        setJPanelProva();
        setJPanelFiltre();
        setJPanelInscripcio();
        setJPanelEstat();
        setJPanelBotons();
        setJPanel();
        pack();
    }// </editor-fold>

    
    
    private void setJPanelProva(){
      GroupLayout jPanelProvaLayout = new GroupLayout(jPanelProva);
        jPanelProva.setLayout(jPanelProvaLayout);
        jPanelProvaLayout.setHorizontalGroup(
            jPanelProvaLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(jPanelProvaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLProva)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTIdProva, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTDescricpcio, GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE))
        );
        jPanelProvaLayout.setVerticalGroup(
            jPanelProvaLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(jPanelProvaLayout.createSequentialGroup()
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanelProvaLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(jLProva)
                    .addComponent(jTIdProva, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTDescricpcio, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
        );
    }
    private void setJPanelFiltre(){
         JTaulaProves.setName("JTaulaProves"); // NOI18N
        jScrollPane1.setViewportView(JTaulaProves);

        GroupLayout jPanelFiltreLayout = new GroupLayout(jPanelFiltre);
        jPanelFiltre.setLayout(jPanelFiltreLayout);
        jPanelFiltreLayout.setHorizontalGroup(
            jPanelFiltreLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFiltreLayout.createSequentialGroup()
                .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 452, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
        );
        jPanelFiltreLayout.setVerticalGroup(
            jPanelFiltreLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFiltreLayout.createSequentialGroup()
                .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );
    }    
    private void setJPanelBotons(){
        GroupLayout jPanelButtonsLayout = new GroupLayout(jPanelBotons);
        jPanelBotons.setLayout(jPanelButtonsLayout);
        jPanelButtonsLayout.setHorizontalGroup(
            jPanelButtonsLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(jPanelButtonsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jBBuscar)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBVeure)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBSortir)
                .addContainerGap(314, Short.MAX_VALUE))
        );
        jPanelButtonsLayout.setVerticalGroup(
            jPanelButtonsLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(GroupLayout.Alignment.TRAILING, jPanelButtonsLayout.createSequentialGroup()
                .addContainerGap(16, Short.MAX_VALUE)
                .addGroup(jPanelButtonsLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(jBBuscar)
                    .addComponent(jBVeure)
                    .addComponent(jBSortir)))
        );

    }    
    private void tancarFinestra(WindowEvent evt){
          dispose();
          System.exit(0);
      }    
    private void setJPanelEstat(){
        GroupLayout jPanelEstatLayout = new GroupLayout(jPanelEstat);
        jPanelEstat.setLayout(jPanelEstatLayout);
        jPanelEstatLayout.setHorizontalGroup(
            jPanelEstatLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(jPanelEstatLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLEstat, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jCEstat, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(198, Short.MAX_VALUE))
        );
        jPanelEstatLayout.setVerticalGroup(
            jPanelEstatLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(jPanelEstatLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(jCEstat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(jLEstat))
        );
    }
    private void setJPanelInscripcio(){
       GroupLayout jPanelInscripcioLayout = new GroupLayout(jPanelInscripcio);
        jPanelInscripcio.setLayout(jPanelInscripcioLayout);
        jPanelInscripcioLayout.setHorizontalGroup(
            jPanelInscripcioLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(jPanelInscripcioLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLdataIniInscripcio, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextIniInscripcio, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jLdataFiInscripcio, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFiInscripcio, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelInscripcioLayout.setVerticalGroup(
            jPanelInscripcioLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(jPanelInscripcioLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(jLdataIniInscripcio)
                .addComponent(jTextFiInscripcio, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(jLdataFiInscripcio)
                .addComponent(jTextIniInscripcio, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        );
    } 
    private void setJPanel(){
        
        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelEstat, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jPanelBotons, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(jPanelInscripcio, GroupLayout.Alignment.LEADING, 0, 482, Short.MAX_VALUE)
                        .addComponent(jPanelProva, GroupLayout.Alignment.LEADING, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanelFiltre, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelBotons, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelProva, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelInscripcio, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelEstat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanelFiltre, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(47, Short.MAX_VALUE))
        );
    }
    private void pintarEtiquetes(){
        jPanelProva.setBorder(BorderFactory.createTitledBorder("Filtrar per Prova"));
        jPanelProva.setName("jPanelProva"); // NOI18N
        jLProva.setText("Prova:");               
        jPanelInscripcio.setBorder(BorderFactory.createTitledBorder("Filtrar per data"));       
        jLdataIniInscripcio.setText("Data Inici Inscripció");       
        jLdataFiInscripcio.setText("Data Inici Inscripció");       
        jPanelEstat.setBorder(BorderFactory.createTitledBorder("Filtrar per Estat"));       
        jLEstat.setText("Estat Prova:");       
        
        jBBuscar.setText("Cercar");
        jBVeure.setText("Veure");       
        jBSortir.setText("Sortir");             
        jPanelFiltre.setBorder(BorderFactory.createTitledBorder("Proves Filtrades")); // NOI18N
        
    }
    private modelDadesPreguntaResposta carregarTaula(Integer nPreguntes){
        String [] columnes={"Pregunta","A","B","C","D"};        
          Object[][] dades=new Object[nPreguntes][5];                          
        modelDadesPreguntaResposta dadesTaula;
          for(int i=0;i<nPreguntes;i++){
            ButtonGroup bG=new ButtonGroup();
            for(int j=0; j<5;j++){
                if(j==0){
                    dades[i][j]=i+1+"";    
                }
                else{                    
                    dades[i][j]=new Boolean(false);                   
                }                
            }                        
        }        
        dadesTaula=new modelDadesPreguntaResposta(columnes,dades,EstatProva.INICIAL);
        return dadesTaula;        
    }    
    
    private void AccioVeureProva(java.awt.event.ActionEvent evt) throws ParseException {
        Prova prova=null;
        
            try {
                if(remotProves==null) remotProves= (RemotProvesInterface) Naming.lookup(url);                
                prova = remotProves.getProva(58);
                this.setVisible(false);
                         new PanelMantenimentGestioProves(prova,remotProves).setVisible(true);
                this.dispose();
                
            } catch (EOlympicsException ex) {
                Logger.getLogger(PantallaBuscadorProves.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NotBoundException ex) {
                Logger.getLogger(PantallaBuscadorProves.class.getName()).log(Level.SEVERE, null, ex);
            } catch (MalformedURLException ex) {
                Logger.getLogger(PantallaBuscadorProves.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RemoteException ex) {
                Logger.getLogger(PantallaBuscadorProves.class.getName()).log(Level.SEVERE, null, ex);
            }
        
        
        
        
        
        
        
    }
    
    public static void main(String args[]) {        
        TDSLanguageUtils.setDefaultLanguage("edu/uoc/tdp/i18n/messages"); 
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PantallaBuscadorProves().setVisible(true);
            }
        });
    }
      
}
/*TableColumn column = null;
for (int i = 0; i < 5; i++) {
    column = table.getColumnModel().getColumn(i);
    if (i == 2) {
        column.setPreferredWidth(100); //third column is bigger
    } else {
        column.setPreferredWidth(50);
    }
}

 */